﻿using UnityEngine;
using System.Collections;

public class WeaponCollection : MonoBehaviour {

	public Transform[] weapons;


	private ArrayList weaponList = new ArrayList();
	private int currentWeaponIndex = 0;

	void Awake()
	{
		//do this beacuse ArrayList is not visible in Unity Editor :(
		foreach (Transform t in weapons)
		{
			weaponList.Add(t);
		}
	}

	// Use this for initialization
	void Start () 
	{
	}

	public Transform getWeaponAtIndex(int index)
	{
		if (index >= weaponList.Count || index < 0)
		{
			return null;
		}

		currentWeaponIndex = index;

		return (Transform)weaponList[index];
	}

	public Transform getWeaponByName(string name)
	{
		return null;
	}

	public Transform getNextWeapon()
	{
		Transform theWeapon = (Transform)weaponList[currentWeaponIndex];
		currentWeaponIndex++;

		if (currentWeaponIndex >= weaponList.Count)
		{
			currentWeaponIndex = 0;
		}

		return theWeapon;
	}

	public Transform getPreviousWeapon()
	{
		int previousIndex = currentWeaponIndex == 0 ? 0 : currentWeaponIndex-1;

		return (Transform)weaponList[previousIndex];
	}

	public bool replaceWeaponWithWeapon(Transform newWeapon, Transform oldWeapon)
	{
		if (weaponList.Contains(oldWeapon))
		{
			int indexOfWeaponToReplace = weaponList.IndexOf(oldWeapon);
			weaponList.RemoveAt(indexOfWeaponToReplace);
			weaponList.Insert(indexOfWeaponToReplace, newWeapon);

			DestroyObject(oldWeapon); //do this here?? mmm

			return true;
		}


		return false;
	}

	public bool addWeaponAtIndex(Transform weapon, int index)
	{
		if (index >= weaponList.Count || index < 0)
		{
			weaponList.Insert(index, weapon);

			return true;
		}
		return false;
	}

	public void addWeapon(Transform weapon)
	{
		weaponList.Add(weapon);
	}

}
